//
//  BYProductDetailsController.m
//  OCDemo
//
//  Created by zhumuzhi on 2022/1/5.
//  商品详情控制器

#import "BYProductDetailsController.h"

#import "BYProductDetailsModel.h"

#import "BYProductDetailsBannerCell.h"
#import "BYProductDetailsInfoCell.h"
#import "BYProductDetailsSubInfoCell.h"
#import "BYProductDetailsCell.h"

#import "DBShopCart.h"
#import "DBShopCartDetail.h"


@interface BYProductDetailsController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) BYTableView           *tableView;

@property (nonatomic, strong) BYProductDetailsModel *detailsModel;

@property (nonatomic, strong) NSMutableArray        *images;

@property (nonatomic, strong) BYButton              *addButton;

@end

@implementation BYProductDetailsController

static NSString *const BYProductDetailsBannerCellID = @"BYProductDetailsBannerCellID";
static NSString *const BYProductDetailsInfoCellID = @"BYProductDetailsInfoCellID";
static NSString *const BYProductDetailsCellID = @"BYProductDetailsCellID";
static NSString *const BYProductDetailsSubInfoCellID = @"BYProductDetailsSubInfoCellID";

#pragma mark - LifeCycle

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = BACKGROUND_VIEW_COLOR;
    [self.view addSubview:self.tableView];
    [self.view addSubview:self.addButton];
    [self requestData];
}

#pragma mark - Event
- (void)addShopCartEvent {
    
    if ([AppConfig isLogin]) {
        NSLog(@"用户已经登录，将商品加入购物车");
        NSMutableArray *loginArray = [kUserDefaultsSington valueForKey:kIsLoginArray];
        NSMutableDictionary *loginDict = [loginArray objectAtIndex:0];
        if (loginDict.count > 0) {
            [self.view makeToast:@"加入购物车成功！"];
            [[DBShopCart sharedInstance] LinkDatabaseAndAddToQueue];
            NSString *userID = [loginDict valueForKey:@"user_name"];
            NSString *cartID =  [[DBShopCart sharedInstance] selectWithUser_Id:userID];
            [[DBShopCartDetail sharedInstance] LinkDatabaseAndAddToQueue];
            [[DBShopCartDetail sharedInstance] insetCart_Id:cartID WithProduct_Id:self.detailsModel.product_id WithNumber:@"1"];
            [[DBShopCartDetail sharedInstance] selectAllMethod];
        }
    }else {
        [self.view makeToast:@"请登录后再操作"];
    }
}

#pragma mark - RequestData
- (void)requestData {
    
    NSArray *modelArray = [BYProductDetailsModel mj_objectArrayWithKeyValuesArray:[kUserDefaultsSington valueForKey:kProductDetail]];
    NSMutableArray *tempImages = [NSMutableArray arrayWithCapacity:0];
    for (BYProductDetailsModel *model in modelArray) {
        if ([model.product_id isEqualToString:self.detailsProductId]) {
            [tempImages addObject:model.topImage1];
            [tempImages addObject:model.topImage2];
            [tempImages addObject:model.topImage3];
            [tempImages addObject:model.topImage4];
            self.detailsModel = model;
        }
    }
    self.images = tempImages;
    [self.tableView reloadData];
}


#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(0 == indexPath.row) {
        BYProductDetailsBannerCell *cell = [tableView dequeueReusableCellWithIdentifier:BYProductDetailsBannerCellID];
        cell.images = self.images;
        return cell;
    }else if(1 == indexPath.row) {
        BYProductDetailsInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:BYProductDetailsInfoCellID];
        cell.model = self.detailsModel;
        return cell;
    }else if(6 == indexPath.row) {
        BYProductDetailsCell *cell = [tableView dequeueReusableCellWithIdentifier:BYProductDetailsCellID];
        cell.model = self.detailsModel;
        return cell;
    }
    BYProductDetailsSubInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:BYProductDetailsSubInfoCellID];
    cell.indexPath = indexPath;
    cell.model = self.detailsModel;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (0 == indexPath.row) {
        return [BYProductDetailsBannerCell cellHeight];
    }else if(1 == indexPath.row){
        return [BYProductDetailsInfoCell cellHeight];
    }else if(8 == indexPath.row) {
        return [BYProductDetailsCell cellHeight];
    }
    return [BYProductDetailsSubInfoCell cellHeight];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

#pragma mark - LazyGet
- (BYTableView *)tableView {
    if(!_tableView) {
        _tableView = [[BYTableView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kSafeAreaTopHeight-kSafeAreaBottomHeight-kAutoWithSize(100)) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[BYProductDetailsBannerCell class] forCellReuseIdentifier:BYProductDetailsBannerCellID];
        [_tableView registerClass:[BYProductDetailsInfoCell class] forCellReuseIdentifier:BYProductDetailsInfoCellID];
        [_tableView registerClass:[BYProductDetailsCell class] forCellReuseIdentifier:BYProductDetailsCellID];
        [_tableView registerClass:[BYProductDetailsSubInfoCell class] forCellReuseIdentifier:BYProductDetailsSubInfoCellID];
    }
    return _tableView;
}

- (BYButton *)addButton {
    if (!_addButton) {
        _addButton = [[BYButton alloc] initWithFrame:CGRectMake(0, kScreenHeight-kSafeAreaTopHeight-kSafeAreaBottomHeight-kAutoWithSize(100), kScreenWidth, kAutoWithSize(50))];
        _addButton.titleLabel.font = MZFont(kFont(14));
        [_addButton setTitle:@"加入购物车" forState:UIControlStateNormal];
        [_addButton setTitleColor:[UIColor colorWithHexString:@"#FFFFFF"] forState:UIControlStateNormal];
        [_addButton setBackgroundColor:PUBLIC_ORANGE_COLOR forState:UIControlStateNormal];
        [_addButton setBackgroundColor:PUBLIC_ORANGE_COLOR forState:UIControlStateSelected];
        [_addButton addTarget:self action:@selector(addShopCartEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}

- (NSMutableArray *)images {
    if (!_images) {
        _images = [NSMutableArray array];
    }
    return _images;
}

@end
